package com.fastadmin.wechat;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fastadmin.config.security.annotation.AnonymousAccess;
import com.fastadmin.mapper.impl.XuQiuMapper;
import com.fastadmin.pojo.FUser;
import com.fastadmin.pojo.XuQiu;
import com.google.common.collect.Maps;
import com.xphsc.easyjdbc.page.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.security.krb5.KdcComm;

import java.util.*;


@RestController
@RequiredArgsConstructor
@Slf4j
public class FXuQiuController extends FBaseController {
    public static final LinkedHashMap<String, Object> typeMap;
    public static final LinkedHashMap<String, Map> apiMap;

    public final XuQiuMapper xuQiuMapper;
    static{
        String s = ResourceUtil.readUtf8Str("classpath:/config/config.json");
        String apiString = ResourceUtil.readUtf8Str("classpath:/config/api.json");
        typeMap = binder.fromJson(s, LinkedHashMap.class);
        apiMap = binder.fromJson(apiString, LinkedHashMap.class);
    }

    @RequestMapping("/f/detail/info")
    @AnonymousAccess
    public Object info(@RequestParam String id, String openid) {

        Object data = typeMap.get(id);
        HashMap<String, Object> resultMap = CollUtil.newHashMap();
        resultMap.put("JZData", false);
        resultMap.put("dataOut", data);
        resultMap.put("result_code", "ok");
        return resultMap;

    }

    @RequestMapping("/f/detail/submit")
    @AnonymousAccess
    public Object submit(@RequestBody String bodyString) {
        Map<String, Object> map = binder.fromJson(bodyString, Map.class);
        HashMap<Object, Object> beanMap = Maps.newHashMap();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String k = entry.getKey().toLowerCase();
            Object v = entry.getValue();
            if (StrUtil.containsAny(StrUtil.toString(v), "[", "]")) {
                String newValue = MapUtil.getStr(map, k);
                newValue = newValue.replace(" ", "");
                newValue = newValue.replace("[", "");
                newValue = newValue.replace("]", "");
                beanMap.put(k, newValue);
            } else {
                beanMap.put(k, v);
            }

        }
        XuQiu xuQiu = binder.fromJson(binder.toJson(beanMap), XuQiu.class);
        Object id = xuQiuMapper.save(xuQiu);
        return ok(id);
    }

    @RequestMapping("/f/detail/orderDetail")
    @AnonymousAccess
    public Object submit(Long id) {
        HashMap<String, Object> result = Maps.newHashMap();
        HashMap<String, Object> DingDanData = Maps.newHashMap();

        XuQiu xuqiu = xuQiuMapper.findById(id);
        DingDanData.put("id", xuqiu.getId());
        DingDanData.put("Type", xuqiu.getType());
        DingDanData.put("Cate_id", xuqiu.getCate_id());
        DingDanData.put("JsonData", xuqiu);
        DingDanData.put("UpDataTime", DateUtil.format(xuqiu.getCreate_time(), "yyyy-MM-dd HH:mm:ss"));
        DingDanData.put("Money", xuqiu.getMoney_other());
        DingDanData.put("ByWho", xuqiu.getName());
        DingDanData.put("openid", xuqiu.getOpenid());
        DingDanData.put("Tel", xuqiu.getMobile());
        DingDanData.put("Address", xuqiu.getAddress());
        DingDanData.put("Lng", xuqiu.getCoordinate().split(",")[0]);
        DingDanData.put("Lat", xuqiu.getCoordinate().split(",")[1]);
        DingDanData.put("state", xuqiu.getStatus());
        Object styleData = apiMap.get(xuqiu.getType()).get("StyleData");
        Object oneConShowEnd = apiMap.get(xuqiu.getType()).get("OneConShowEnd");
        result.put("StyleData", styleData);
        result.put("OneConShowEnd", oneConShowEnd);
        result.put("result_code", "ok");
        result.put("DingDanData", DingDanData);
        return result;
    }

    @RequestMapping("/f/detail/list")
    @AnonymousAccess
    public Object list(String openid) {
        HashMap<String, Object> result = Maps.newHashMap();
        ArrayList<Map> maps = CollUtil.newArrayList();

        PageInfo<XuQiu> pageInfo = xuQiuMapper.findByOpenId(openid);
        List<XuQiu> list = pageInfo.list;
        for (XuQiu xuqiu : list) {
            HashMap<String, Object> DingDanData = Maps.newHashMap();
            DingDanData.put("id", xuqiu.getId());
            DingDanData.put("Type", xuqiu.getType());
            DingDanData.put("Cate_id", xuqiu.getCate_id());
            DingDanData.put("JsonData", xuqiu);
            DingDanData.put("UpDataTime", DateUtil.format(xuqiu.getCreate_time(), "yyyy-MM-dd HH:mm:ss"));
            DingDanData.put("Money", xuqiu.getMoney_other());
            DingDanData.put("ByWho", xuqiu.getName());
            DingDanData.put("openid", xuqiu.getOpenid());
            DingDanData.put("Tel", xuqiu.getMobile());
            DingDanData.put("Address", xuqiu.getAddress());
            DingDanData.put("Lng", xuqiu.getCoordinate().split(",")[0]);
            DingDanData.put("Lat", xuqiu.getCoordinate().split(",")[1]);
            DingDanData.put("state", xuqiu.getStatus());
            maps.add(DingDanData);
        }
        result.put("result_code", "ok");
        result.put("DingDanData", maps);

        return result;
    }

    @RequestMapping("/f/detail/updateStatus")
    @AnonymousAccess
    public Object updateStatus(Long id, Integer value) {
        HashMap<String, Object> result = Maps.newHashMap();
        XuQiu xuqiu = XuQiu.builder().id(id).status(value).build();
        xuQiuMapper.update(xuqiu);
        result.put("result_code", "ok");
        return result;
    }



}
